package com.midea.ai.air.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 体检自定义动画
 * 
 * @author qinchuo
 *
 */
public class CheckProgressView extends View
{
	private int widthTop = 0, widthBottom,mHeight;
	private boolean reStart;
	private int baseLine = 200;
	private int baseLineSmall = 10;
	public boolean finish;
	public boolean biger = false;
	public boolean bigerBaseLine = false;
	private int mColor;

	public CheckProgressView(Context context)
	{
		super(context);
	}

	public CheckProgressView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	//0 绿色，1 红色
	public void setProgress(int progress,int height,boolean reSet,int color)
	{
		mColor = color;
		if (reSet) 
		{
			mHeight = height;
			reStart = true;
			widthTop = 0;
			baseLine = 200;
			baseLineSmall = 10;
			finish = false;
			biger = false;
			bigerBaseLine = false;
		}
		widthBottom = progress;
		int w = getWidth();
		if (reStart)
		{
			if (!biger)
			{
				widthTop = widthTop + (w / 80);
			}
			else
			{
				widthTop = widthBottom;
			}
			
			if (widthTop > widthBottom)
			{
				if (!biger)
				{
					widthTop = widthBottom;
					if (widthTop > 200)
					{
						bigerBaseLine = true;
					}
				}
				biger = true;
				finish = true;
			}
		}
		else
		{
			widthTop = 0; 
		}
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		Paint mPaint = new Paint();
		Paint mPaint1 = new Paint();
		Paint mPaint2 = new Paint();
		Paint mPaintLine = new Paint();
		int w = getWidth();
		int h = getHeight();
		Rect clientRect = new Rect(0, 0, widthTop, h);
		Rect clientRect2 = new Rect(0, 0, widthBottom, h);
		Rect clientRect0 = new Rect(widthTop, 0, w, h);
		
		int colors[] = new int[3];
		float positions[] = new float[3];
		
		// 第1个点
		colors[0] = 0x55ffffff;
		positions[0] = 0;

		// 第2个点
		colors[1] = 0x55ffffff;
		positions[1] = 0.5f;

		// 第3个点
		colors[2] = 0x55ffffff;
		positions[2] = 1;
		
		int colorsTop[] = new int[3];
		float positionsTop[] = new float[3];
		
		// 第1个点
		colorsTop[0] = 0x00ffffff;
		positionsTop[0] = 0;

		// 第2个点
		colorsTop[1] = 0x33ffffff;
		positionsTop[1] = 0.1f;

		// 第3个点
		colorsTop[2] = 0x66ffffff;
		positionsTop[2] = 1;
		
		if (mColor != 0)
		{
			mPaint.setColor(Color.parseColor("#FF4500"));
		}
		else
		{
			mPaint.setColor(Color.parseColor("#13c400"));
		}
		canvas.drawRect(clientRect0, mPaint);
		
		LinearGradient shader2 = new LinearGradient(0, 0, widthBottom, 0,
				colors, positions, LinearGradient.TileMode.CLAMP);
		mPaint2.setShader(shader2);
		
		LinearGradient shader1 = new LinearGradient(0, 0, widthBottom, 0,
				colorsTop, positionsTop, LinearGradient.TileMode.CLAMP);
		mPaint1.setShader(shader1);

		int colorsLine[] = new int[3];
		float positionsLine[] = new float[3];

		if (mColor == 0)
		{
			// 第1个点
			colorsLine[0] = 0xff13c400;
			positionsLine[0] = 0;

			// 第2个点
			if (bigerBaseLine)
			{
				if (biger)
				{
					if ((baseLine = baseLine - 20) > 0)
					{
						Log.d("ProgressView", "baseLine = " + baseLine);
						
						positionsLine[1] = (float) ((float)(widthTop - baseLine) / widthTop);
						colorsLine[1] = 0xff13c400;
						
						if (baseLine == 180)
						{
							// 第3个点
							colorsLine[2] = 0x3313c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 160)
						{
							// 第3个点
							colorsLine[2] = 0x4413c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 140)
						{
							// 第3个点
							colorsLine[2] = 0x5513c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 120)
						{
							// 第3个点
							colorsLine[2] = 0x7713c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 100)
						{
							// 第3个点
							colorsLine[2] = 0x9913c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 80)
						{
							// 第3个点
							colorsLine[2] = 0xbb13c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 60)
						{
							// 第3个点
							colorsLine[2] = 0xcc13c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 40)
						{
							// 第3个点
							colorsLine[2] = 0xdd13c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 20)
						{
							// 第3个点
							colorsLine[2] = 0xee13c400;
							positionsLine[2] = 1.0f;
						}
						else
						{
							// 第3个点
							colorsLine[2] = 0xff13c400;
							positionsLine[2] = 1.0f;
						}
					}
					else
					{
						positionsLine[1] = (float) ((float)(widthTop - 200) / widthTop);
						colorsLine[1] = 0xff13c400;
						
						// 第3个点
						colorsLine[2] = 0xff13c400;
						positionsLine[2] = 1.0f;
					}
				}
				else
				{
					positionsLine[1] = (float) ((float)(widthTop - 200) / widthTop);
					colorsLine[1] = 0xff13c400;
					
					// 第3个点
					colorsLine[2] = 0x3313c400;
					positionsLine[2] = 1.0f;
				}
			}
			else
			{
				if (biger)
				{
					
					if ((baseLineSmall = baseLineSmall - 1) > 0)
					{
						positionsLine[1] = (float) ((float)(10.0 - (float)baseLineSmall) / 10.0);
						colorsLine[1] = 0xff13c400;
						
						if (baseLineSmall == 9)
						{
							// 第3个点
							colorsLine[2] = 0x3313c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 8)
						{
							// 第3个点
							colorsLine[2] = 0x4413c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 7)
						{
							// 第3个点
							colorsLine[2] = 0x5513c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 6)
						{
							// 第3个点
							colorsLine[2] = 0x7713c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 5)
						{
							// 第3个点
							colorsLine[2] = 0x9913c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 4)
						{
							// 第3个点
							colorsLine[2] = 0xaa13c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 3)
						{
							// 第3个点
							colorsLine[2] = 0xcc13c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 2)
						{
							// 第3个点
							colorsLine[2] = 0xdd13c400;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 1)
						{
							// 第3个点
							colorsLine[2] = 0xee13c400;
							positionsLine[2] = 1.0f;
						}
						else
						{
							// 第3个点
							colorsLine[2] = 0xff13c400;
							positionsLine[2] = 1.0f;
						}
					}
					else
					{
						positionsLine[1] = 0.01f;
						colorsLine[1] = 0xff13c400;
						
						// 第3个点
						colorsLine[2] = 0xff13c400;
						positionsLine[2] = 1.0f;
					}
				}
				else
				{
					positionsLine[1] = 0.01f;
					colorsLine[1] = 0xff13c400;
					
					// 第3个点
					colorsLine[2] = 0x3313c400;
					positionsLine[2] = 1.0f;
				}
			}
		}
		else
		{
			// 第1个点
			colorsLine[0] = 0xffff4500;
			positionsLine[0] = 0;

			// 第2个点
			if (bigerBaseLine)
			{
				if (biger)
				{
					if ((baseLine = baseLine - 20) > 0)
					{
						Log.d("ProgressView", "baseLine = " + baseLine);
						
						positionsLine[1] = (float) ((float)(widthTop - baseLine) / widthTop);
						colorsLine[1] = 0xffff4500;
						
						if (baseLine == 180)
						{
							// 第3个点
							colorsLine[2] = 0x33ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 160)
						{
							// 第3个点
							colorsLine[2] = 0x44ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 140)
						{
							// 第3个点
							colorsLine[2] = 0x55ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 120)
						{
							// 第3个点
							colorsLine[2] = 0x77ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 100)
						{
							// 第3个点
							colorsLine[2] = 0x99ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 80)
						{
							// 第3个点
							colorsLine[2] = 0xbbff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 60)
						{
							// 第3个点
							colorsLine[2] = 0xccff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 40)
						{
							// 第3个点
							colorsLine[2] = 0xddff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLine == 20)
						{
							// 第3个点
							colorsLine[2] = 0xeeff4500;
							positionsLine[2] = 1.0f;
						}
						else
						{
							// 第3个点
							colorsLine[2] = 0xffff4500;
							positionsLine[2] = 1.0f;
						}
					}
					else
					{
						positionsLine[1] = (float) ((float)(widthTop - 200) / widthTop);
						colorsLine[1] = 0xffff4500;
						
						// 第3个点
						colorsLine[2] = 0xffff4500;
						positionsLine[2] = 1.0f;
					}
				}
				else
				{
					positionsLine[1] = (float) ((float)(widthTop - 200) / widthTop);
					colorsLine[1] = 0xffff4500;
					
					// 第3个点
					colorsLine[2] = 0x33ff4500;
					positionsLine[2] = 1.0f;
				}
			}
			else
			{
				if (biger)
				{
					
					if ((baseLineSmall = baseLineSmall - 1) > 0)
					{
						positionsLine[1] = (float) ((float)(10.0 - (float)baseLineSmall) / 10.0);
						colorsLine[1] = 0xffff4500;
						
						if (baseLineSmall == 9)
						{
							// 第3个点
							colorsLine[2] = 0x33ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 8)
						{
							// 第3个点
							colorsLine[2] = 0x44ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 7)
						{
							// 第3个点
							colorsLine[2] = 0x55ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 6)
						{
							// 第3个点
							colorsLine[2] = 0x77ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 5)
						{
							// 第3个点
							colorsLine[2] = 0x99ff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 4)
						{
							// 第3个点
							colorsLine[2] = 0xaaff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 3)
						{
							// 第3个点
							colorsLine[2] = 0xccff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 2)
						{
							// 第3个点
							colorsLine[2] = 0xddff4500;
							positionsLine[2] = 1.0f;
						}
						else if (baseLineSmall == 1)
						{
							// 第3个点
							colorsLine[2] = 0xeeff4500;
							positionsLine[2] = 1.0f;
						}
						else
						{
							// 第3个点
							colorsLine[2] = 0xffff4500;
							positionsLine[2] = 1.0f;
						}
					}
					else
					{
						positionsLine[1] = 0.01f;
						colorsLine[1] = 0xffff4500;
						
						// 第3个点
						colorsLine[2] = 0xffff4500;
						positionsLine[2] = 1.0f;
					}
				}
				else
				{
					positionsLine[1] = 0.01f;
					colorsLine[1] = 0xffff4500;
					
					// 第3个点
					colorsLine[2] = 0x33ff4500;
					positionsLine[2] = 1.0f;
				}
			}
		}

		Log.d("ProgressView", "colorsLine[1] = " + colorsLine[1] + ",positionsLine[1] = " +positionsLine[1]);
		Log.d("ProgressView", "colorsLine[2] = " + colorsLine[2] + ",positionsLine[2] = " +positionsLine[2]);

		LinearGradient shaderLine = new LinearGradient(0, 0, widthTop, 0, colorsLine,
				positionsLine, LinearGradient.TileMode.CLAMP);
		mPaintLine.setShader(shaderLine);

//		int lineY = 10;
//		for (int i = 0; i < mHeight / 10; i++)
//		{
//			Rect clientRect3 = new Rect(0, lineY, widthTop, lineY + 2);
//			canvas.drawRect(clientRect3, mPaintLine);
//
//			lineY = lineY + 10;
//		}

//		int lineX = 10;
//		for (int i = 0; i < widthTop / 10; i++)
//		{
//			Rect clientRect4 = new Rect(lineX, 0, lineX + 2, h);
//			canvas.drawRect(clientRect4, mPaintLine);
//
//			lineX = lineX + 10;
//		}
		canvas.drawRect(clientRect, mPaintLine);
		canvas.drawRect(clientRect, mPaint1);
		canvas.drawRect(clientRect2, mPaint2);
	}
}
